package org.etlt;

import org.apache.commons.lang3.StringUtils;

public interface SettingCheck {

    void check();

    default void assertNotNull(String message, Object... objs) {
        boolean isNull = true;
        for (Object obj : objs) {
            isNull = (obj == null);
            if (!isNull)
                break;
        }
        if (isNull)
            throw new IllegalArgumentException("unsupported null object: " + message);
    }

    default void assertNotNull(String message, String... strings) {
        for (String s : strings) {
            if (StringUtils.isEmpty(s))
                throw new IllegalArgumentException("unsupported entry String: " + message);
        }
    }

    default void assertNotNull(String message, Object obj) {
        if (obj == null)
            throw new IllegalArgumentException("unsupported null object: " + message);
    }



    /**
     * if condition is false, assert failed
     *
     * @param message
     * @param condition
     */
    default void assertFalse(String message, boolean condition) {
        if (!condition) {
            throw new IllegalArgumentException("condition should be true: " + message);
        }
    }

    /**
     * if condition is true, assert failed
     *
     * @param message
     * @param condition
     */
    default void assertTrue(String message, boolean condition) {
        assertFalse(message, !condition);
    }

    default void assertName(String message, String name) {
        assertNotNull(message, name);
        checkNameValidity(name);
    }

    default void checkNameValidity(String name) {
        String m = "illegal name: "+ "[" + name + "]";
        char[] chars = name.toCharArray();
        if (chars.length > 0 && !Character.isJavaIdentifierStart(chars[0]))
            throw new IllegalArgumentException(m);
        for (int i = 1; i < chars.length; i++) {
            if (!Character.isJavaIdentifierPart(chars[i]))
                throw new IllegalArgumentException(m);
        }
    }

}
